package com.epalmpay.service.jobservice.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.entity.SysTaskLog;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.mapper.GroupAccountLogDayMapper;
import com.epalmpay.mapper.GroupAccountLogDaySumMapper;
import com.epalmpay.mapper.GroupAccountLogMonthMapper;
import com.epalmpay.mapper.GroupAccountLogMonthSumMapper;
import com.epalmpay.mapper.SysTaskLogMapper;
import com.epalmpay.service.jobservice.ISaleAccountStatisService;

/**
 * Created by wzq on 2017/6/2.
 */
@Service("saleAccountStatisService")
public class SaleAccountStatisServiceImpl implements ISaleAccountStatisService{
    @Resource
    private GroupAccountLogDayMapper groupAccountLogDayMapper;
    @Resource
    private GroupAccountLogDaySumMapper groupAccountLogDaySumMapper;
    @Resource
    private GroupAccountLogMonthMapper groupAccountLogMonthMapper;
    @Resource
    private GroupAccountLogMonthSumMapper groupAccountLogMonthSumMapper;
    @Resource
    private SysTaskLogMapper sysTaskLogMapper;    
    
    
        
    public int saveSaleAccountDayStatis(Date statisDate) throws Exception{
    	int result=0;
    	//查询老记录
    	Map<String,Object> param=new HashMap<String,Object>();
		param.put("taskType", TaskType.ChannelSaleAccountDayStatis.getType());
		param.put("taskDate", statisDate);

		SysTaskLog oldRecord = sysTaskLogMapper.selectSysTaskLogByTypeAndDate(param);
        if(oldRecord  != null && oldRecord.getResultStatus() == RunStatus.Success.getType()){
        	throw new ApplicationException(Rescode.FAIL,"计划任务已经执行成功！");
        }

    	StringBuffer sb = new StringBuffer();
    	result = groupAccountLogDayMapper.insertDayStatis(statisDate);
    	sb.append("本次任务日统计影响的行数：").append(result);
    	result = groupAccountLogDaySumMapper.insertDaySumStatis(statisDate);
    	sb.append(";本次任务日sum统计影响的行数：").append(result);
    	
    	//插入计划任务成功日志
        if(oldRecord  == null){
	    	SysTaskLog record = new SysTaskLog();
	    	record.setGmtCreate(new Date());
	    	record.setGmtModified(new Date());
	    	record.setTaskType(TaskType.ChannelSaleAccountDayStatis.getType());
	    	record.setTaskDate(statisDate);
	    	record.setResultStatus(RunStatus.Success.getType());
	    	record.setRunTimes(0);
	    	record.setMemo(sb.toString());
	    	result = sysTaskLogMapper.insert(record);
        }
        else{
        	oldRecord.setGmtModified(new Date());
        	oldRecord.setResultStatus(RunStatus.Success.getType());
        	oldRecord.setRunTimes((oldRecord.getRunTimes()==null?0:oldRecord.getRunTimes())+1);
        	oldRecord.setMemo(sb.toString());
        	result = sysTaskLogMapper.updateByPrimaryKeySelective(oldRecord);
        }
    	
    	return result;
    }
    
    public int saveSaleAccountMonthStatis(Date statisDate) throws Exception{
    	int result=0;
    	Map<String,Object> param=new HashMap<String,Object>();
		param.put("taskType", TaskType.ChannelSaleAccountMonthStatis.getType());
		param.put("taskDate", statisDate);

		SysTaskLog oldRecord = sysTaskLogMapper.selectSysTaskLogByTypeAndDate(param);
        if(oldRecord  != null && oldRecord.getResultStatus() == RunStatus.Success.getType()){
        	throw new ApplicationException(Rescode.FAIL,"计划任务已经执行成功！");
        }

    	StringBuffer sb = new StringBuffer();
    	result = groupAccountLogMonthMapper.insertMonthStatis(statisDate);
    	sb.append("本次任务月统计影响的行数：").append(result);
    	result = groupAccountLogMonthSumMapper.insertMonthSumStatis(statisDate);
    	sb.append(";本次任务月sum统计影响的行数：").append(result);
    	
    	//插入计划任务成功日志
        if(oldRecord  == null){
	    	SysTaskLog record = new SysTaskLog();
	    	record.setGmtCreate(new Date());
	    	record.setGmtModified(new Date());
	    	record.setTaskType(TaskType.ChannelSaleAccountMonthStatis.getType());
	    	record.setTaskDate(statisDate);
	    	record.setResultStatus(RunStatus.Success.getType());
	    	record.setRunTimes(0);
	    	record.setMemo(sb.toString());
	    	result = sysTaskLogMapper.insert(record);
        }
        else{
        	oldRecord.setGmtModified(new Date());
        	oldRecord.setResultStatus(RunStatus.Success.getType());
        	oldRecord.setRunTimes((oldRecord.getRunTimes()==null?0:oldRecord.getRunTimes())+1);
        	oldRecord.setMemo(sb.toString());
        	result = sysTaskLogMapper.updateByPrimaryKeySelective(oldRecord);
        }
    	
    	return result;
    }
}
